package GoodsPackage;

import UsersPackage.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/alterGood.do")
public class AlterGoodServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        try {
            String goods_id = request.getParameter("goods_id");
            String goods_name = request.getParameter("goods_name");
            String category = request.getParameter("category");
            String unitprice = request.getParameter("unitprice");
            int stock = Integer.parseInt(request.getParameter("stock"));
            String factory = request.getParameter("factory");
            String photo = request.getParameter("photo");
            String details = request.getParameter("details");

            GoodsDao goodsDao = new GoodsDao();

            // Assuming you have a method in UserDao for adding a new user
            if (goodsDao.updateGoodByGoodid(goods_id, goods_name, category, unitprice, stock, factory, photo, details)>0) {
                response.getWriter().print("修改商品成功，在3秒后跳转回上一页面!");
                response.setHeader("Refresh", "3;url=menu/device/2-1.jsp"); // 在3秒后跳转回登录页面
            } else {
                response.getWriter().print("修改商品失败，在3秒后跳转回上一页面");
                response.setHeader("Refresh", "3;url=menu/device/2-1.jsp"); // 在3秒后跳转回登录页面
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
